/*-------------------------------------------------------------------------
 *
 * pg_trigger.h
 *	  "触发器" 系统目录 (pg_trigger) 的定义
 *
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * src/include/catalog/pg_trigger.h
 *
 * NOTES
 *	  Catalog.pm 模块读取此文件并推导模式信息。
 *
 *-------------------------------------------------------------------------
 */
#ifndef PG_TRIGGER_H
#define PG_TRIGGER_H

#include "catalog/genbki.h"
#include "catalog/pg_trigger_d.h"

/* ----------------
 *		pg_trigger 定义。cpp 将其转换为
 *		typedef struct FormData_pg_trigger
 *
 * 注意：当 tgconstraint 非零时，tgconstrrelid、tgconstrindid、
 * tgdeferrable 和 tginitdeferred 基本上与引用的
 * pg_constraint 条目冗余。然而，非延迟触发器可以与延迟约束关联。
 * ----------------
 */
CATALOG(pg_trigger,2620,TriggerRelationId)
{
	Oid			oid;			/* oid */
	Oid			tgrelid BKI_LOOKUP(pg_class);	/* 触发器附加到的关系 */
	Oid			tgparentid BKI_LOOKUP_OPT(pg_trigger);	/* 父触发器的 OID（如果有的话） */
	NameData	tgname;			/* 触发器的名称 */
	Oid			tgfoid BKI_LOOKUP(pg_proc); /* 要调用的函数的 OID */
	int16		tgtype;			/* BEFORE/AFTER/INSTEAD，UPDATE/DELETE/INSERT，
 * ROW/STATEMENT；见下文 */
	char		tgenabled;		/* 关于 session_replication_role 的触发器触发配置 */
	bool		tgisinternal;	/* 触发器是系统生成的 */
	Oid			tgconstrrelid BKI_LOOKUP_OPT(pg_class); /* 约束的来源表（如果有的话） */
	Oid			tgconstrindid BKI_LOOKUP_OPT(pg_class); /* 约束的
 * 支持索引（如果有的话） */
	Oid			tgconstraint BKI_LOOKUP_OPT(pg_constraint); /* 相关的
 * pg_constraint 条目（如果有的话） */
	bool		tgdeferrable;	/* 约束触发器是可延迟的 */
	bool		tginitdeferred; /* 约束触发器初始为延迟 */
	int16		tgnargs;		/* tgargs 中的额外参数数量 */

	/*
	 * 可变长度字段从这里开始，但我们允许直接访问
	 * tgattr。注意：tgattr 和 tgargs 不能为空。
	 */
	int2vector	tgattr BKI_FORCE_NOT_NULL;	/* 列号，如果触发器
 * 在列上 */

#ifdef CATALOG_VARLEN
	bytea		tgargs BKI_FORCE_NOT_NULL;	/* first\000second\000tgnargs\000 */
	pg_node_tree tgqual;		/* WHEN 表达式，如果没有则为 NULL */
	NameData	tgoldtable;		/* 旧的过渡表，如果没有则为 NULL */
	NameData	tgnewtable;		/* 新的过渡表，如果没有则为 NULL */
#endif
} FormData_pg_trigger;

/* ----------------
 *		Form_pg_trigger 对应指向具有
 *		pg_trigger 关系格式的元组的指针。
 * ----------------
 */
typedef FormData_pg_trigger *Form_pg_trigger;

DECLARE_TOAST(pg_trigger, 2336, 2337);

DECLARE_INDEX(pg_trigger_tgconstraint_index, 2699, TriggerConstraintIndexId, on pg_trigger using btree(tgconstraint oid_ops));
DECLARE_UNIQUE_INDEX(pg_trigger_tgrelid_tgname_index, 2701, TriggerRelidNameIndexId, on pg_trigger using btree(tgrelid oid_ops, tgname name_ops));
DECLARE_UNIQUE_INDEX_PKEY(pg_trigger_oid_index, 2702, TriggerOidIndexId, on pg_trigger using btree(oid oid_ops));

DECLARE_ARRAY_FOREIGN_KEY((tgrelid, tgattr), pg_attribute, (attrelid, attnum));

#ifdef EXPOSE_TO_CLIENT_CODE

/* Bits within tgtype */
#define TRIGGER_TYPE_ROW				(1 << 0)
#define TRIGGER_TYPE_BEFORE				(1 << 1)
#define TRIGGER_TYPE_INSERT				(1 << 2)
#define TRIGGER_TYPE_DELETE				(1 << 3)
#define TRIGGER_TYPE_UPDATE				(1 << 4)
#define TRIGGER_TYPE_TRUNCATE			(1 << 5)
#define TRIGGER_TYPE_INSTEAD			(1 << 6)

#define TRIGGER_TYPE_LEVEL_MASK			(TRIGGER_TYPE_ROW)
#define TRIGGER_TYPE_STATEMENT			0

/* 注意，TRIGGER_TYPE_TIMING_MASK中的位不相邻 */
#define TRIGGER_TYPE_TIMING_MASK \
	(TRIGGER_TYPE_BEFORE | TRIGGER_TYPE_INSTEAD)
#define TRIGGER_TYPE_AFTER				0

#define TRIGGER_TYPE_EVENT_MASK \
	(TRIGGER_TYPE_INSERT | TRIGGER_TYPE_DELETE | TRIGGER_TYPE_UPDATE | TRIGGER_TYPE_TRUNCATE)

/* 操作tgtype的宏 */
#define TRIGGER_CLEAR_TYPE(type)		((type) = 0)

#define TRIGGER_SETT_ROW(type)			((type) |= TRIGGER_TYPE_ROW)
#define TRIGGER_SETT_STATEMENT(type)	((type) |= TRIGGER_TYPE_STATEMENT)
#define TRIGGER_SETT_BEFORE(type)		((type) |= TRIGGER_TYPE_BEFORE)
#define TRIGGER_SETT_AFTER(type)		((type) |= TRIGGER_TYPE_AFTER)
#define TRIGGER_SETT_INSTEAD(type)		((type) |= TRIGGER_TYPE_INSTEAD)
#define TRIGGER_SETT_INSERT(type)		((type) |= TRIGGER_TYPE_INSERT)
#define TRIGGER_SETT_DELETE(type)		((type) |= TRIGGER_TYPE_DELETE)
#define TRIGGER_SETT_UPDATE(type)		((type) |= TRIGGER_TYPE_UPDATE)
#define TRIGGER_SETT_TRUNCATE(type)		((type) |= TRIGGER_TYPE_TRUNCATE)

#define TRIGGER_FOR_ROW(type)			((type) & TRIGGER_TYPE_ROW)
#define TRIGGER_FOR_BEFORE(type)		(((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_BEFORE)
#define TRIGGER_FOR_AFTER(type)			(((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_AFTER)
#define TRIGGER_FOR_INSTEAD(type)		(((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_INSTEAD)
#define TRIGGER_FOR_INSERT(type)		((type) & TRIGGER_TYPE_INSERT)
#define TRIGGER_FOR_DELETE(type)		((type) & TRIGGER_TYPE_DELETE)
#define TRIGGER_FOR_UPDATE(type)		((type) & TRIGGER_TYPE_UPDATE)
#define TRIGGER_FOR_TRUNCATE(type)		((type) & TRIGGER_TYPE_TRUNCATE)

/*
 * 高效的宏，用于检查tgtype是否匹配特定级别（TRIGGER_TYPE_ROW或TRIGGER_TYPE_STATEMENT）、时间（TRIGGER_TYPE_BEFORE、TRIGGER_TYPE_AFTER或TRIGGER_TYPE_INSTEAD）和事件（TRIGGER_TYPE_INSERT、TRIGGER_TYPE_DELETE、TRIGGER_TYPE_UPDATE或TRIGGER_TYPE_TRUNCATE）。注意，tgtype可以匹配多个事件，但只能匹配一个级别或时间。
 */
#define TRIGGER_TYPE_MATCHES(type, level, timing, event) \
	(((type) & (TRIGGER_TYPE_LEVEL_MASK | TRIGGER_TYPE_TIMING_MASK | (event))) == ((level) | (timing) | (event)))

/*
 * 用于确定是否为触发器指定了tgnewtable或tgoldtable的宏。
 */
#define TRIGGER_USES_TRANSITION_TABLE(namepointer) \
	((namepointer) != (char *) NULL)

#endif							/* EXPOSE_TO_CLIENT_CODE */

#endif							/* PG_TRIGGER_H */
